#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 2e5 + 10;

int n;
int v1[N], v2[N];
PII q[N];

/*
逆向思考
寻找满足条件的三个问题可以通过反向思考;
首先思考随机枚举3个问题一共有多少种 n * (n - 1) * (n - 2) / 6
然后发现满足条件的问题状态太多了，反向思考不满足条件的点，约束变多发现只有这种情况才是不满足条件的{[a1, x], [a1, b1], [y, b1]}
当中间的[a1, b1] 被固定住时，剩下两个点的种数为 (cnta1 - 1) * (cntb1 - 1)
*/
void solve(){
    cin >> n;
    for(int i = 0; i <= n; i ++) v1[i] = v2[i] = 0;

    for(int i = 1, a, b; i <= n; i ++){
        cin >> a >> b;
        q[i] = {a, b};
        v1[a] ++, v2[b] ++;
    }

    LL res = (LL) n * (n - 1) * (n - 2) / 6;

    for(int i = 1; i <= n; i ++){
        LL k1 = v1[q[i].first] - 1, k2 = v2[q[i].second] - 1;
        res -= k1 * k2;
    }

    cout << res << '\n';

}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--){
        solve();
    }
    return 0;
}